//
//  JEHVideoCollectionViewCell.m
//  SinaNews
//
//  Created by 蒋恩浩 on 15/12/8.
//  Copyright © 2015年 JEH. All rights reserved.
//

#import "JEHVideoCollectionViewCell.h"

@interface JEHVideoCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *dataSource;
@property (nonatomic, retain) UITableView *tableView;

@end

@implementation JEHVideoCollectionViewCell

- (void)dealloc
{
    _delegate = nil;
    [_dataSource release];
    [_tableView release];
    [_url release];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
    }
    return self;
}

- (void)setUrl:(NSString *)url {
    if (_url != url) {
        [_url release];
        _url = [url retain];
        [self.tableView reloadData];
        [self getData];
    }
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];
    self.tableView.frame = self.bounds;
}

#pragma getData
- (void)getData {
    
    if (_url) {
        
        self.dataSource = [NSMutableArray arrayWithCapacity:0];
        [JEHNetWorkTool getWithUrl:self.url parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
            NSDictionary *dic = result[@"data"];
            NSArray *array = dic[@"list"];
            for (NSDictionary *dic in array) {
                
                JEHRootModel *root = [[JEHRootModel alloc] initWithDictionary:dic];
                [self.dataSource addObject:root];
                [root release];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.tableView reloadData];
            });
        } fail:^(NSError *error) {
            
            NSLog(@"没网~~~~~~");
        }];
    }
}

#pragma mark - createTableView
- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView nightMode];
    _tableView.backgroundColor = [UIColor lightGrayColor];
    [_tableView registerClass:[JEHVideoTableViewCell class] forCellReuseIdentifier:@"JEHVideoTableViewCellIdentifier"];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCellIdentifier"];
    [self addSubview:self.tableView];

#pragma mark - ReloadData
    NSNotificationCenter *centers = [NSNotificationCenter defaultCenter];
    [centers addObserver:self selector:@selector(receiveNotification:) name:@"ReloadData" object:nil];
}

#pragma mark - ReloadData
- (void)receiveNotification:(NSNotification *)notification {
    
    [self.tableView reloadData];
}

#pragma mark - UITableViewDelegateMethod
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (_dataSource) {
        
        return self.dataSource.count;
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_dataSource) {
        
        if (indexPath.section < self.dataSource.count) {
            JEHRootModel *root = self.dataSource[indexPath.section];
            JEHVideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JEHVideoTableViewCellIdentifier"];
            [cell.titleLabel nightMode];
            [cell nightMode];
            cell.rootModel = root;
            return cell;
        }
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellIdentifier"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section != self.dataSource.count - 1) {
        return 5;
    }
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 250;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    JEHRootModel *root = self.dataSource[indexPath.section];
    [self.delegate playVideoWithUrl:root.videoModel.url];
}



@end














